<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  
  <meta name="description" content="dectobin// 本题要求实现一个函数，将正整数n转换为二进制后输出。// 函数接口定义：// void dectobin( int n );// 函数dectobin应在一行中打印出二进制的n。// 建议用递归实现。// 输入样例：// 10// 输出样例：// 1010" />
  

  
  
  
  
  
  
  <title>递归实现十进制转二进制 | 土木工程在读。。</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="dectobin&#x2F;&#x2F; 本题要求实现一个函数，将正整数n转换为二进制后输出。&#x2F;&#x2F; 函数接口定义：&#x2F;&#x2F; void dectobin( int n );&#x2F;&#x2F; 函数dectobin应在一行中打印出二进制的n。&#x2F;&#x2F; 建议用递归实现。&#x2F;&#x2F; 输入样例：&#x2F;&#x2F; 10&#x2F;&#x2F; 输出样例：&#x2F;&#x2F; 1010">
<meta property="og:type" content="article">
<meta property="og:title" content="递归实现十进制转二进制">
<meta property="og:url" content="https://civilprogrammer.gitee.io/2020/04/03/%E9%80%92%E5%BD%92%E5%AE%9E%E7%8E%B0%E5%8D%81%E8%BF%9B%E5%88%B6%E8%BD%AC%E4%BA%8C%E8%BF%9B%E5%88%B6/index.html">
<meta property="og:site_name" content="土木工程在读。。">
<meta property="og:description" content="dectobin&#x2F;&#x2F; 本题要求实现一个函数，将正整数n转换为二进制后输出。&#x2F;&#x2F; 函数接口定义：&#x2F;&#x2F; void dectobin( int n );&#x2F;&#x2F; 函数dectobin应在一行中打印出二进制的n。&#x2F;&#x2F; 建议用递归实现。&#x2F;&#x2F; 输入样例：&#x2F;&#x2F; 10&#x2F;&#x2F; 输出样例：&#x2F;&#x2F; 1010">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2020-04-02T16:00:19.000Z">
<meta property="article:modified_time" content="2020-05-17T07:58:58.000Z">
<meta property="article:author" content="CivilProgrammer">
<meta name="twitter:card" content="summary">
  
  
    <link rel="icon" href="/CivilProgrammer/css/images/favicon.ico">
  
  
<link rel="stylesheet" href="/CivilProgrammer/css/style.css">

  

  
  <!-- baidu webmaster push -->
  <script src='//push.zhanzhang.baidu.com/push.js'></script>
<meta name="generator" content="Hexo 5.0.0"></head>
<body class="home blog custom-background custom-font-enabled single-author">
  <div id="page" class="hfeed site">
      <header id="masthead" class="site-header" role="banner">
    <hgroup>
      <h1 class="site-title">
        <a href="/CivilProgrammer/" title="土木工程在读。。" rel="home">土木工程在读。。</a>
      </h1>
      
        <h2 class="site-description hitokoto"></h2>
        <script type="text/javascript" src="https://v1.hitokoto.cn/?encode=js"></script>
      
    </hgroup>

    <nav id="site-navigation" class="main-navigation" role="navigation">
            <button class="menu-toggle">菜单</button>
            <a class="assistive-text" href="/#content" title="跳至内容">跳至内容</a><!--TODO-->
            <div class="menu-main-container">
                <ul id="menu-main" class="nav-menu">
                
                    <li class="menu-item menu-item-type-post_type menu-item-object-page"><a href="/CivilProgrammer/">Home</a></li>
                
                    <li class="menu-item menu-item-type-post_type menu-item-object-page"><a href="/CivilProgrammer/archives">Archives</a></li>
                
                </ul>
            </div>
    </nav>
</header>

      <div id="main" class="wrapper">
        <div id="primary" class="site-content"><div id="content" role="main"><article id="post-递归实现十进制转二进制" class="post-递归实现十进制转二进制 post type-post status-publish format-standard hentry">
    <!---->

      <header class="entry-header">
        
        
  
    <h1 class="entry-title article-title">
      递归实现十进制转二进制
    </h1>
  

        
        <div class="comments-link">
            
            <a href="javascript:void(0);" data-url="https://civilprogrammer.gitee.io/2020/04/03/%E9%80%92%E5%BD%92%E5%AE%9E%E7%8E%B0%E5%8D%81%E8%BF%9B%E5%88%B6%E8%BD%AC%E4%BA%8C%E8%BF%9B%E5%88%B6/" data-id="ckl9naiig000ln8vl4y6v3j0e" class="leave-reply bdsharebuttonbox" data-cmd="more">Share</a>
        </div><!-- .comments-link -->
      </header><!-- .entry-header -->

    <div class="entry-content">
      
        <h2 id="dectobin"><a href="#dectobin" class="headerlink" title="dectobin"></a>dectobin</h2><p>// 本题要求实现一个函数，将正整数n转换为二进制后输出。<br>// 函数接口定义：<br>// void dectobin( int n );<br>// 函数dectobin应在一行中打印出二进制的n。<br>// 建议用递归实现。<br>// 输入样例：<br>// 10<br>// 输出样例：<br>// 1010</p>
<a id="more"></a>
<p>// 裁判测试程序样例：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">dectobin</span><span class="params">( <span class="keyword">int</span> n )</span></span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">int</span> n;</span><br><span class="line"></span><br><span class="line">    <span class="built_in">scanf</span>(<span class="string">&quot;%d&quot;</span>, &amp;n);</span><br><span class="line">    dectobin(n);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">dectobin</span><span class="params">( <span class="keyword">int</span> n )</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (n == <span class="number">0</span> || n == <span class="number">1</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;%d&quot;</span>, n);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">else</span></span><br><span class="line">    &#123;</span><br><span class="line">        dectobin(n / <span class="number">2</span>);  </span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;%d&quot;</span>, n % <span class="number">2</span>); <span class="comment">// dectobin(n % 2);  </span></span><br><span class="line">    &#125;                     </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>// 类似于十进制数，/10 和 %10，这两个操作分别把一个n位数分成前n-1位和最后1位，<br>// 最后一位数，在十进制中，对10取模就是它本身，同样，二进制下，最后一位数<br>// 对2取模就是它本身。然后对前n-1位数继续调用函数   </p>
<hr>

      
    </div><!-- .entry-content -->

    <footer class="entry-meta">
    <a href="/CivilProgrammer/2020/04/03/%E9%80%92%E5%BD%92%E5%AE%9E%E7%8E%B0%E5%8D%81%E8%BF%9B%E5%88%B6%E8%BD%AC%E4%BA%8C%E8%BF%9B%E5%88%B6/">
    <time datetime="2020-04-02T16:00:19.000Z" class="entry-date">
        2020-04-03
    </time>
</a>
    
    
    </footer>
</article>


    
<nav class="nav-single">
    <h3 class="assistive-text">文章导航</h3>
    
        <span class="nav-previous"><a href="/CivilProgrammer/2020/04/09/EnglishWriting1/" rel="prev"><span class="meta-nav">←</span> EnglishWriting(1)</a></span>
    
    
        <span class="nav-next"><a href="/CivilProgrammer/2020/04/02/%E8%AE%B0%E5%BD%95%E7%AC%AC%E4%B8%80%E6%AC%A1%E6%8A%98%E8%85%BE%E6%A1%8C%E9%9D%A2/" rel="next">记录第一次折腾桌面 <span class="meta-nav">→</span></a></span>
    
</nav><!-- .nav-single -->







</div></div>
        <div id="secondary" class="widget-area" role="complementary">
  
    <aside id="search" class="widget widget_search"><form role="search" method="get" accept-charset="utf-8" id="searchform" class="searchform" action="//google.com/search">
    <div>
        <input type="text" value="" name="s" id="s" />
        <input type="submit" id="searchsubmit" value="搜索" />
    </div>
</form></aside>
  
    
  
    
  
    
  <aside class="widget">
    <h3 class="widget-title">Recents</h3>
    <div class="widget-content">
      <ul>
        
          <li>
            <a href="/CivilProgrammer/2021/02/18/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E6%9F%A5%E6%89%BE%E7%AB%A0%E8%8A%82%E4%B9%A0%E9%A2%98%E5%88%B7%E5%AE%8C/">数据结构查找章节习题刷完</a>
          </li>
        
          <li>
            <a href="/CivilProgrammer/2021/02/13/vmware%E4%B8%ADcentos7%E4%B8%8BNAT%E6%A8%A1%E5%BC%8F%E8%AE%BE%E7%BD%AE%E9%9D%99%E6%80%81ip/">vmware中centos7下NAT模式设置静态ip</a>
          </li>
        
          <li>
            <a href="/CivilProgrammer/2020/12/31/2020-12-31/">2020-12-31</a>
          </li>
        
          <li>
            <a href="/CivilProgrammer/2020/12/06/VSCode%E4%B8%AD%E6%96%87%E8%BE%93%E5%87%BA%E4%B9%B1%E7%A0%81/">VSCode中文输出乱码</a>
          </li>
        
          <li>
            <a href="/CivilProgrammer/2020/10/10/%E5%BB%BA%E5%B7%A5%E7%B2%BE%E7%A5%9E%E4%BC%A0%E6%89%BF%E8%80%85%E8%AF%84%E9%80%89%E2%80%94%E2%80%94%E5%8B%A4%E5%A5%8B%E5%A5%BD%E5%AD%A6/">(no title)</a>
          </li>
        
      </ul>
    </div>
  </aside>

  
    
  
    
  
</div>
      </div>
      <footer id="colophon" role="contentinfo">
    <p>&copy; 2021 CivilProgrammer
    All rights reserved.</p>
    <p>Powered by <a href="http://hexo.io/" target="_blank">Hexo</a></p>
</footer>
    <script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"1","bdMiniList":false,"bdPic":"","bdStyle":"2","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='/js/share.js'];</script>

<script src="/js/jquery-3.3.1.min.js"></script>


  
<link rel="stylesheet" href="/CivilProgrammer/fancybox/jquery.fancybox.css">

  
<script src="/CivilProgrammer/fancybox/jquery.fancybox.pack.js"></script>




<script src="/CivilProgrammer/js/script.js"></script>


<script src="/js/navigation.js"></script>

<div id="bg"></div>

  </div>
</body>
</html>